package servlet;



import model.resources;
import model.student;
import model.teacher;
import model.users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@WebServlet(value = "/Login")
public class Login extends HttpServlet{

    public static boolean flag=false;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String uc=req.getParameter("user");

        String un=req.getParameter("username");

        String upw=req.getParameter("password");


        //Cookie ck=new Cookie(un,un);

        if(uc!=null && un!=null && upw!=null && (!uc.isEmpty())&&(!un.isEmpty())&&(!upw.isEmpty())){
            users user=new users(un,upw,uc);

            if(user.findPswByUaccount(user)){
                req.getSession().setAttribute("using",un);
                req.setAttribute("msg","登陆成功！");
                flag=true;


/*                Set<Map.Entry<String,String>> s= hashMap.entrySet();
                for (Map.Entry<String,String> temp:s){
                    System.out.println(temp.getKey()+"---"+temp.getValue());
                }*/

                HashMap<String,String> hashMap= resources.findResourceByCate(uc);
                req.setAttribute("s",hashMap);
                req.setAttribute("un",un);
                req.getRequestDispatcher("/jsp/pro.jsp").forward(req,resp);

                System.out.println("----->"+un);
            }else{
                req.setAttribute("msg","账号或者密码或身份不正确，请重新登陆！");
                req.getRequestDispatcher("/index.jsp").forward(req,resp);

            }
        }else{
            req.setAttribute("msg","请填写完整信息！");
            req.getRequestDispatcher("/index.jsp").forward(req,resp);

        }




    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
